package com.emotionsns;

import java.util.Vector;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.view.MotionEvent;
import android.view.View;

public class Ball extends View implements Runnable {

	final int DEFAULT_BALL_SPEED = 1;

	BallGroup ballGroup;
	Point location;
	int distanceX;
	int distanceY;
	int ballRadius;
	int touchRadius;
	Context context;
	int[] windowsize;
	boolean isRunning;
	IEmotion emotion;

	int[] color = { Color.CYAN, Color.GRAY, Color.LTGRAY, Color.RED, Color.MAGENTA, Color.YELLOW,
			Color.BLUE };
	int setcolor;

	public Ball(Context context, BallGroup ballGroup) {
		super(context);
		this.context = context;
		this.ballGroup = ballGroup;
		windowsize = ballGroup.windowsize();
		if (windowsize[0] > windowsize[1]) {
			ballRadius = windowsize[1] / 40;
			touchRadius = windowsize[1] / 10;
			this.location = new Point((int) (Math.random() * windowsize[1]) - ballRadius,
					(int) (Math.random() * windowsize[0]) - ballRadius);
		} else {
			ballRadius = windowsize[0] / 40;
			touchRadius = windowsize[0] / 10;
			this.location = new Point((int) (Math.random() * windowsize[0]) - ballRadius,
					(int) (Math.random() * windowsize[1]) - ballRadius);
		}
		setcolor = (int) (Math.random() * color.length);

	}

	@Override
	protected void onDraw(Canvas canvas) {
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setAlpha(100);
		paint.setColor(color[setcolor]);
		canvas.drawCircle(location.x, location.y, ballRadius / 2, paint);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		float x = event.getX();
		float y = event.getY();

		if (location.x - touchRadius < x && (location.x + touchRadius) > x
				&& location.y - touchRadius < y && (location.y + touchRadius) > y) {
			ballGroup.isTouchEventFromBall = true;
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				ballGroup.touchedBall = this;
				Intent intent = new Intent(context, TranslucentBlurTextActivity.class);
				intent.putExtra("twit", "ball id=" + emotion.getContent());
				context.startActivity(intent);
				break;
			}
		} else {
			return false;
		}
		return true;
	}

	@Override
	public void run() {
		while (isRunning) {
			setupDirection();
			location.x += distanceX;
			location.y += distanceY;
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
			}
		}
	}

	public void setRunning(boolean isRunning) {
		this.isRunning = isRunning;
		if (!isRunning) {
			Thread.currentThread().interrupt();
		}
	}

	private void setupDirection() {
		Vector<Ball> ballList = ballGroup.balls;

		for (int i = 0; i < ballList.size(); i++) {
			Ball activeBall = ballList.get(i);

			if (activeBall != this) {
				if ((activeBall.location.x - (activeBall.ballRadius + ballRadius) / 2) < location.x
						&& activeBall.location.x > location.x) {
					if ((activeBall.location.y - (activeBall.ballRadius + ballRadius) / 2) < location.y
							&& activeBall.location.y > location.y) {
						distanceX = -DEFAULT_BALL_SPEED;
						distanceY = -DEFAULT_BALL_SPEED;
					} else if ((activeBall.location.y + (activeBall.ballRadius + ballRadius) / 2) > location.y
							&& activeBall.location.y < location.y) {
						distanceX = -DEFAULT_BALL_SPEED;
						distanceY = DEFAULT_BALL_SPEED;
					}
				} else if ((activeBall.location.x + (activeBall.ballRadius + ballRadius) / 2) > location.x
						&& activeBall.location.x < location.x) {
					if ((activeBall.location.y - (activeBall.ballRadius + ballRadius) / 2) < location.y
							&& activeBall.location.y > location.y) {
						distanceX = DEFAULT_BALL_SPEED;
						distanceY = -DEFAULT_BALL_SPEED;
					} else if ((activeBall.location.y + (activeBall.ballRadius + ballRadius) / 2) > location.y
							&& activeBall.location.y < location.y) {
						distanceX = DEFAULT_BALL_SPEED;
						distanceY = DEFAULT_BALL_SPEED;
					}
				}
			}
		}

		if (location.x > windowsize[0] - ballRadius)
			distanceX = -DEFAULT_BALL_SPEED;
		if (location.x - ballRadius < 0)
			distanceX = DEFAULT_BALL_SPEED;
		if (location.y > windowsize[1] - ballRadius)
			distanceY = -DEFAULT_BALL_SPEED;
		if (location.y - ballRadius < 0)
			distanceY = DEFAULT_BALL_SPEED;
	}
}
